package com.jf.service.impl;

import com.jf.bean.Role;
import com.jf.bean.Userinfo;
import com.jf.dao.RoleDao;
import com.jf.dao.UserinfoDao;
import com.jf.dao.impl.RoleDaoImpl;
import com.jf.dao.impl.UserinfoDaoImpl;
import com.jf.service.RoleService;
import com.jf.util.PageInfo;
import com.jf.util.ResultJson;

import java.util.List;

public class RoleServiceImpl implements RoleService {
    private RoleDao roleDao = new RoleDaoImpl();
    private UserinfoDao userinfoDao = new UserinfoDaoImpl();
    @Override
    public List<Role> getAll() {
        return roleDao.getAll();
    }

    @Override
    public ResultJson getSelect() {
        return ResultJson.success(roleDao.getAll());// list集合放入data属性中
    }

    @Override
    public ResultJson getPage(PageInfo pageInfo, Role role) {
        List<Role> list = roleDao.getPage(pageInfo,role);
        int count = roleDao.getCount(role);
        pageInfo.setPageData(list,count);
        return ResultJson.success(pageInfo);
    }

    @Override
    public ResultJson saveOrUpdate(Role role) {
        int result;
        if(role.getId() == null){
            result = roleDao.addSelect(role);
        }else{
            result = roleDao.updateSelect(role);
        }
        return result > 0 ?ResultJson.success():ResultJson.error("操作失败");
    }

    @Override
    public ResultJson delete(int[] ids) {
        List<Userinfo> list = userinfoDao.getUserinfoByRoleIds(ids);
        if(!list.isEmpty()){
            return ResultJson.error("编号【"+list.get(0).getRoleId()+"】已被使用，无法删除");
        }
        int result = roleDao.batchDelete(ids);
        return result == ids.length ? ResultJson.success() : ResultJson.error("删除异常！");
    }

    @Override
    public ResultJson info(int id) {
        return ResultJson.success(roleDao.getById(id));
    }
}
